'use strict';
angular.module('app.controllers').controller('LoginCtrl', function($scope, $rootScope, $state, $stateParams, $window, UserService, ngDialog, HotelService) {
    var vm = this;

    vm.getHotelName = function() {

        if (vm.hotel.code && vm.hotel.code.length == 5) {
            vm.hotelName = 'Loading...';
            UserService.getHotelName(vm.hotel.code).then(function(ret) {
                vm.hotelName = 'Loading...';
                if (ret.data.code == 0) {
                    vm.hotelName = ret.data.value;
                } else {
                    vm.hotelName = 'Not found.';
                }
            })
        }
    }

    function getEditable(arr, month, year) {
        var ret = [];
        var today = new Date();
        for (var i = 0; i < arr.length; i++)
            if (arr[i]["year"] == year && arr[i]["month"] == month) {
                $window.sessionStorage.aEditable = arr[i]["annual"];
                return (today.getDate() <= arr[i]["end"] && today.getDate() >= arr[i]["start"]);
            }
        return false;
    }

    vm.forget = function() {
        ngDialog.open({
            template: 'views/forget_password.html',
            ariaAuto: false,
            className: 'ngdialog-theme-plain custom-width',
            scope: $scope
        })
    }

    vm.login = function() {
        vm.errorMsg = '';
        if (vm.hotel == null || vm.hotel.password == null || vm.hotel.code == null) {
            vm.errorMsg = 'Please input holidex code and password.';
        } else {
            var user = {
                'userName': vm.hotel.code,
                'password': hex_md5(vm.hotel.password)
            }
            UserService.login(user).then(function(ret) {
                if (ret.data.loginSuccess == true) {
                    if (vm.hotel.password == '88888888') {
                        $scope.user = ret.data;
                        ngDialog.open({
                            template: 'views/change_password.html',
                            ariaAuto: false,
                            className: 'ngdialog-theme-plain custom-width',
                            scope: $scope,
                            preCloseCallback: function(value) {
                                vm.hotel.password = '';
                                return true;
                            }
                        })
                        return;
                    }
                    vm.hotel = ret.data.hotel;
                    $window.sessionStorage.logged = true;
                    $window.sessionStorage.userRole = ret.data.role;
                    if (ret.data.role == 'ADMIN') {
                        $window.sessionStorage.hotelName = ret.data.code;
                        $window.sessionStorage.userRole = 'Admin';
                        $window.sessionStorage.subrole = ret.data.subrole;
                        $window.sessionStorage.region = ret.data.region;
                        HotelService.getCurrentMonth().then(function(ret) {
                            $window.sessionStorage.month = ret.data.value.month;
                            $window.sessionStorage.year = ret.data.value.year;
                            if ($window.sessionStorage.subrole == 'Training Admin') {
                                $state.go('admin.training', null, {
                                    reload: true
                                });
                            } else if ($window.sessionStorage.subrole == 'GC People Admin') {
                                $state.go('admin.people', null, {
                                    reload: true
                                });
                            } else {
                                $state.go('admin.main', null, {
                                    reload: true
                                });
                            }

                        });


                    } else {
                        $window.sessionStorage.hotelName = ret.data.hotel.name;
                        $window.sessionStorage.hotelId = ret.data.hotel.id;
                        $window.sessionStorage.isTop = ret.data.hotel.top20;
                        $window.sessionStorage.brand = ret.data.hotel.brand;
                        HotelService.getCurrentMonth().then(function(ret) {
                            $window.sessionStorage.month = ret.data.value.month;
                            $window.sessionStorage.year = ret.data.value.year;
                            $window.sessionStorage.userRole = vm.hotel.type;
                            HotelService.getSettings().then(function(response) {
                                $scope.configure = response.data.value;
                                $window.sessionStorage.editable = getEditable($scope.configure, $window.sessionStorage.month, $window.sessionStorage.year);
                                if (vm.hotel.type != 'PreOpening') {
                                    $state.go('home.hr.main', null, {
                                        reload: true
                                    });
                                } else {
                                    $state.go('home.preopen.main', null, {
                                        reload: true
                                    });
                                }
                            });

                        })
                    }
                } else {
                    vm.errorMsg = ret.data.errorMessage;
                }
            })



        };

    }
})
